var _ctFfmpegInstanceM2A = null; function _ctEnsureFfmpegM2A() { if (_ctFfmpegInstanceM2A) return Promise.resolve(_ctFfmpegInstanceM2A); return loadScriptPromise('https://unpkg.com/\u0040ffmpeg/ffmpeg\u00400.11.6/dist/ffmpeg.min.js').then(function() { var ns = window.FFmpeg || {}; var createFFmpeg = ns.createFFmpeg; var fetchFile = ns.fetchFile; if (!createFFmpeg) throw new Error('FFmpeg library failed to load'); var ffmpeg = createFFmpeg({ log: false, corePath: 'https://unpkg.com/\u0040ffmpeg/core\u00400.11.0/dist/ffmpeg-core.js' }); _ctFfmpegInstanceM2A = { ffmpeg: ffmpeg, fetchFile: fetchFile }; return ffmpeg.load().then(function(){ return _ctFfmpegInstanceM2A; }); }); } function processFile(blob, fileName) { var raw = parseInt(($('.extra-input input').first().val() || '192'), 10); if (!isFinite(raw)) raw = 192; var bitrate = Math.max(64, Math.min(320, raw)); var ext = ((fileName || '').toLowerCase().split('.').pop() || 'mp4'); var inName = 'in.' + ext; _ctEnsureFfmpegM2A().then(function(ctx) { var ffmpeg = ctx.ffmpeg, fetchFile = ctx.fetchFile; return fetchFile(blob).then(function(buf) { try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', 'out.mp3'); } catch (e) {} ffmpeg.FS('writeFile', inName, buf); return ffmpeg.run('-i', inName, '-vn', '-c:a', 'libmp3lame', '-b:a', bitrate + 'k', 'out.mp3').then(function() { var data = ffmpeg.FS('readFile', 'out.mp3'); var outBlob = new Blob([data.buffer], { type: 'audio/mpeg' }); var base = (fileName || 'audio').replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(outBlob), base + '.mp3'); try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', 'out.mp3'); } catch (e) {} }); }); }).catch(function(err) { alert('Could not extract MP3 audio from this video: ' + (err && err.message || err)); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }